<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Klaim_kartu_garansi extends CI_Controller {

		function Klaim_kartu_garansi()
		{
				parent::__construct();
				$this->load->library('session');
				$this->load->library('form_validation');
				$this->load->library('encrypt');
				$this->load->model('muserrole');
				$this->load->model('mmaster');
				$this->load->model('mglobal');
				$this->load->model('mklaim');
		}
		
		function index()
		{
				redirect(base_url().'index.php/guarantee/create');
				// $this->outlet();

		}
		
		function outlet()
		{
				
				$data['getRegion'] = $this->mklaim->getTerritory();
				$data['getProgram'] = $this->mklaim->getProgram();
				$data['getProduct'] = $this->mklaim->getProduct();
				$userlogin = $this->session->userdata('username');
				$dblokal = $this->load->database("default", TRUE);
				$territory_id = $dblokal->get( "f_get_cluster('$userlogin') ")->row()->f_get_cluster;

				if(!$territory_id || $territory_id == 1) {
					redirect('index.php/cluster');
				}
				$pid = $this->mglobal->showdata("parent_id","t_mtr_territory",array("territory_id"=> $territory_id ) ,"dblokal");
				$data['cluster'] = $this->mglobal->showdata("territory_name","t_mtr_territory",array("user_id"=>$this->session->userdata('username')),"dblokal");
				$data['region'] = $this->mglobal->showdata("territory_name","t_mtr_territory",array("territory_id"=>$pid),"dblokal");
				
				$data['tanggal'] = date('Y-m-d');
				$data['template'] = "shell/smooth";
				$data['main_view']      = 'f-klaim-kartu-garansi-outlet';
				$this->load->view($data['template'],$data);
		}

		function saveUserRoleAction(){
			$group = $this->input->post("groupid");
			$menu  = $this->input->post("menuid");
			$actid = $this->input->post("actid");
			$nilai = $this->input->post("nilai");
			$this->muserrole->saveUserRoleAction($group,$menu,$nilai,$actid);

				echo json_encode(array(
					"status"=>"OK",
					"pesan" =>"Success"
				));
				return; 
    }
		
		
		function showcluster(){
			$nilai = $this->input->post('nilai');
			
			$pesan="<select name=cluster id=cluster>
					<option value=''>Pilih</option>";
											
			if($nilai<>""){
				$getData = $this->mklaim->getTerritory($nilai);
				foreach($getData->result() as $r){
					$pesan.='<option value="'.$r->territory_id.'">'.$r->territory_name.'</option>';
				}
			}
			$pesan.="</select>";
			
			echo json_encode(array(
				"status"=>"OK",
				"pesan" =>$pesan
			));
			return; 
		}
		
		function showsales($nilai='',$val=""){
				
			$url = parse_url($_SERVER['REQUEST_URI']);
                if(count($url)>1){
						parse_str($url['query'], $params);
						$nilai = $params['term'];
				}
				
			if($val=="full"){
				$ad = explode("%20::%20", $nilai);
				$nilai = $ad[0];
			}
			$getData = $this->mklaim->getSales($nilai,$val);
			
			echo json_encode($getData);
			return; 
		}
		
		function showoutlet($nilai='',$val=""){
				
			$url = parse_url($_SERVER['REQUEST_URI']);
                if(count($url)>1){
						parse_str($url['query'], $params);
						$nilai = $params['term'];
				}
			
			if($val=="full"){
				$ad = explode("%20::%20", $nilai);
				$nilai = $ad[0];
			}
			$getData = $this->mklaim->getOutlet($nilai,$val);
			
			echo json_encode($getData);
			return; 
		}
		
		function showproduk($nilai='',$val=""){
				
			$url = parse_url($_SERVER['REQUEST_URI']);
                if(count($url)>1){
						parse_str($url['query'], $params);
						$nilai = $params['term'];
				}
			$getData = $this->mklaim->getProduk($nilai);
			
			echo json_encode($getData);
			return; 
		}
		
		function showagent(){
			$nilai = $this->input->post('nilai');
			
			$pesan="<select name=outlet id=outlet>
					<option value=''>Pilih</option>";
											
			if($nilai<>""){
				$getData = $this->mklaim->getTerritory($nilai,5);
				foreach($getData->result() as $r){
					$pesan.='<option value="'.$r->territory_id.'">'.$r->territory_name.'</option>';
				}
			}
			$pesan.="</select>";
			
			echo json_encode(array(
				"status"=>"OK",
				"pesan" =>$pesan
			));
			return; 
		}
		
		function showklaim(){
			$nilai = $this->input->post('nilai');
			
			$getProduct = $this->mklaim->getProduct();
			$pesan='<table width=700px>';
			for($i=1;$i<=$nilai;$i++){
				$pesan.='<tr>
							<td width="450px" class=t_isi>Product: <input type=hidden value='.$i.' name=idpr>
								<select name="product" id="product"><option>Pilih</option>';
								foreach($getProduct->result() as $r){
										$pesan.='<option value="'.$r->item_name.'">'.$r->item_name.'</option>';
								}
								$pesan.='</select></td>
							<td width="250px" class=t_isi id="divMDN'.$i.'">MDN: <select name="product"><option>Pilih</option></select></td>
						</tr>';
			}
			$pesan.='</table>';
			
			echo json_encode(array(
				"status"=>"OK",
				"pesan" =>$pesan
			));
			return; 
		}
		
		function showmdn($nilai=""){
			//$nilai = $this->input->post('nilai');
			
			$getData = $this->mklaim->getMDN($nilai);
			echo json_encode($getData);
			return; 
		}
		
		function showproductmdn($nilai=""){
			//$nilai = $this->input->post('nilai');
			
			$getData = $this->mklaim->getProductMDN($nilai);
			echo json_encode($getData);
			return; 
		}
		
		function showfrontliner(){
			$nilai = $this->input->post('nilai');
			
			$pesan="<select name=frontliner id=frontliner>
					<option value=''>Pilih</option>";
											
			if($nilai<>""){
				$getData = $this->mklaim->getfrontliner($nilai);
				foreach($getData->result() as $r){
					$pesan.='<option value="'.$r->outlet_frontliner_id.'">'.$r->frontliner_name.'</option>';
				}
			}
			$pesan.="</select>";
			$pesan.='<a href="'.base_url().'"><img id="quantityImg" height="20px" style="margin-left:1%;" src="'. base_url() .'file/js/easyui/themes/icons/addRow.png" /></a>';
			//<span id='dialog-form-open'><img src='".base_url()."file/images/add.png' style='cursor:pointer;'></span>";
			
			echo json_encode(array(
				"status"=>"OK",
				"pesan" =>$pesan
			));
			return; 
		}
		
		function savefrontliner(){
				$this->mklaim->saveFrontliner();
				$this->showfrontliner();
		}
		
		function saveklaimoutlet(){
			
			$sales	= $this->input->post('sales');
			$outlet	= $this->input->post('outlet');
			$frontliner= $this->input->post('frontliner');
			$tanggal= $this->input->post('tanggal');
			$program= 10;//$this->input->post('program');
			
			//result array
			$mdn = $this->input->post('mdn');
			
			$sl = explode(" :: ", $sales);
			$ou = explode(" :: ", $outlet);
			$xsales = $this->mglobal->showdata('user_id','t_mtr_user',array('user_id'=>$sl[0]),'dblokal');
			$xoutlet = $this->mglobal->showdata('outlet_id','t_mtr_outlet',array('outlet_id'=>$ou[0]),'dblokal');
			
			if($sales=="" ||  $outlet == "" || $program==""  || ($sales<>""&&$xsales=="") || ($outlet<>""&&$xoutlet=="") ){
				$pesan = count($mdn)."Terdapat kesalahan \n";
				if($sales==""){
						$pesan .="=> Sales belum diisi \n";
				}else{
						if($xsales ==""){
								$pesan .="=> Kode Sales ".$sales." tidak ditemukan \n";
						}
				}
				if($outlet == "" ){
						$pesan .="=> Outlet belum diisi \n";
				}else{
						if($xoutlet ==""){
								$pesan .="=> Kode Outlet ".$outlet." tidak ditemukan \n";
						}
				}
				if($frontliner == "" ){
						//$pesan .="=> Frontliner belum dipilih \n";
				}
				if( $program=="" ){
						$pesan .="=> Program belum dipilih \n";
				}
				echo json_encode(array(
						"status"=>"ERROR",
						"pesan" =>$pesan
					));
					return; 
			}else{
				#jika data sudah benar, insert data
				$this->mklaim->saveKlaim();
				$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
				echo json_encode(array(
						"status"=>"OK",
						"pesan" =>"Data berhasil disimpan"
					));
					return; 
			}
			
		}
		
		function read(){
				echo json_encode( $this->mklaim->read() );
		}
		function edit(){
				
		}
		function delete(){
				
		}
}